from numpy import *
import numpy as np

class REGIME:
    def __init__(self,NormalizeMatrix,Weight,Gangliang):
        '''
        :param NormalizeMatrix: 矩阵
        :param Weight: 权重
        :param Gangliang: 属性的成本或效益型
        '''
        self.Matrix=NormalizeMatrix
        self.Weight=Weight
        self.lines=len(NormalizeMatrix)
        self.colums=len(NormalizeMatrix[0])
        self.Ganglinag=Gangliang

    def getTemp(self):
        '''
        辅助建立REGIME Matrix的工具，记录Elf小标f,l的小标
        :return:
        '''
        Temp = []
        for i in range(self.lines):
            for j in range(self.lines):
                if i != j:
                    Temp.append(i * 10 + j)
        return Temp

    def getREGIMEMatrix(self):
        '''
        建立REGIME Matrix，根据钢量比较每两个备选方案中每个属性好坏，
        Af,Al两个备选方案，属性C1，...Cn;
        如果Af中的属性C1比Al中的属性好，矩阵中记为+1,反之记为-1.
        :return:
        '''
        RM = [[0 for i in range(self.colums)]for j in range(self.lines*(self.lines-1))]
        Temp = self.getTemp()
        for m in range(len(Temp)):
            for j in range(len(self.Weight)):
                f = Temp[m] // 10
                l = Temp[m] % 10
                if self.Ganglinag[j] >0:
                    if self.Matrix[f][j] < self.Matrix[l][j]:
                        RM[m][j] = -1
                    if self.Matrix[f][j] > self.Matrix[l][j]:
                        RM[m][j] = 1
                else:
                    if self.Matrix[f][j] < self.Matrix[l][j]:
                        RM[m][j] = 1
                    if self.Matrix[f][j] > self.Matrix[l][j]:
                        RM[m][j] = -1
        return RM

    def getSuperiorityIdentifier(self):
        '''
        根据REGIME Matrix矩阵求Superiority Identifier
        :return:
        '''
        RM = self.getREGIMEMatrix()
        SI = [0 for i in range(len(RM))]
        for i in range(len(RM)):
            for j in range(len(RM[0])):
                if RM[i][j]>0:
                    SI[i] += self.Weight[j]
        return SI

    def getGuideIndex(self):
        '''
        根据REGIME Matrix矩阵求Guide Index
        :return:
        '''
        RM = self.getREGIMEMatrix()
        GI = [0 for i in range(len(RM))]
        for i in range(len(RM)):
            for j in range(len(self.Weight)):
                GI[i]+= RM[i][j]*self.Weight[j]
        return GI

    def max(self,Ke):
        '''
        求所有备选方案的最高分，的下标
        :param Ke:
        :return:
        '''
        max = Ke[0]
        index = 0
        for i in range(1,len(Ke)):
            if max < Ke[i]:
                max = Ke[i]
                index = i
        return index

    def getResult(self):
        '''
        The first technique
        :return:
        '''
        GI = self.getGuideIndex()
        Temp = self.getTemp()
        Ke = [0 for i in range(self.lines)]#就是为了记录每个备选方案的分数，分数越高越好
        for i in range(len(GI)):
            if GI[i] >0:
                Ke[Temp[i]//10]+=1
            if GI[i]<0:
                Ke[Temp[i]%10]+=1
        index = self.max(Ke)
        RE = self.Matrix[index]
        return RE

    def getResult1(self):
        '''
        The second technique
        :return:
        '''
        SI = self.getSuperiorityIdentifier()
        Temp = self.getTemp()
        Ke = [0 for k in range(self.lines)]#就是为了记录每个备选方案的分数，分数越高越好
        for i in range(len(SI)):
            f = Temp[i]//10
            l = Temp[i]%10
            np = l*10+f
            for j in range(len(Temp)):
                if Temp[j] == np:
                    te = j
            if SI[i] - SI[te] > 0:
                Ke[Temp[i] // 10] += 1
            if SI[i] - SI[te] < 0:
                Ke[Temp[i] % 10] += 1
        index = self.max(Ke)
        RE = self.Matrix[index]
        return RE

if __name__=="__main__":
    matrix_1 = [[0.710, 4.100, 0.740, 0.310, 0.420, 0.830],
                    [0.710, 4.100, 0.180, 0.720, 0.990, 0.250],
                    [1.330, 5.900, 0.740, 0.310, 0.420, 0.830],
                    [1.450, 4.900, 0.270, 0.650, 0.420, 0.440]] # 决策矩阵
    # 属性权重
    Weight = [0.171, 0.185, 0.177, 0.225, 0.157, 0.085]
    Gangliang=[1 for i in range(len(Weight))]
    print("The first technique(第一种方法):")
    RE1 = REGIME(matrix_1, Weight, Gangliang)
    result1 = RE1.getResult()
    print('最优备选方案：')
    print(result1)
    print("The second technique(第二种方法):")
    RE2 = REGIME(matrix_1, Weight, Gangliang)
    result2 = RE2.getResult1()
    print('最优备选方案：')
    print(result2)

